<template>
  <div id="app">
    <MainCanvas />
    <button @click="addImage">添加元素</button>
    <button @click="changeImage">替换元素</button>
  </div>
</template>

<script>
import { fabric } from "fabric";
import MainCanvas from "./components/MainCanvas.vue";
import appContext from "./appContext";

const IMG1 = "//img.alicdn.com/tfs/TB13ZfrLAY2gK0jSZFgXXc5OFXa-150-150.png";
const IMG2 =
  "//gma.alicdn.com/15847751/O1CN01RwNAES2780WMK2v28_!!0-saturn_solar.jpg_200x200.jpg";

export default {
  name: "App",
  components: {
    MainCanvas,
  },
  methods: {
    addImage() {
      fabric.Image.fromURL(IMG1, function (oImg) {
        oImg.set({
          left: 100,
          top: 100,
        });
        oImg.setControlVisible('ml',false);
        oImg.setControlVisible('mr',false);
        
        appContext.canvas.add(oImg);
      });
    },
    changeImage() {
      const elem = appContext.canvas.getActiveObject();
      elem &&
        elem.setSrc(IMG2, () => {
          appContext.canvas.renderAll();
        });
    },
  },
};
</script>

<style>
body {
  background-color: #eee;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  width: 750px;
  margin: 0 auto;
}
</style>
